using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoBehaviour
{
    public static UIManager Instance;
     private void Awake() 
     {
        Instance = this;  
        uiDic = new Dictionary<string, UIBase>();
     }
     public Dictionary<string,UIBase> uiDic;

     public T Show<T>(string uiName)where T:UIBase
     {
        if(uiDic.ContainsKey(uiName))
        {
            T t = uiDic[uiName] as T;
            t.OnShow();
            return t;
        }
        else
        {
            GameObject obj = Instantiate(Resources.Load("UI/"+uiName),transform) as GameObject;
            T t = obj.GetComponent<T>();
            uiDic.Add(uiName,t);
            return t;
        }
     }

     public T GetUI<T>(string uiName)where T:UIBase
     {
        return uiDic[uiName] as T;
     }

     public void Close(string uiName)
     {
        if(uiDic.ContainsKey(uiName))
        {
            UIBase ui = uiDic[uiName];
            Destroy(ui.gameObject);
            uiDic.Remove(uiName);
        }
     }

     public void Hide(string uiName)
     {
        if(uiDic.ContainsKey(uiName))
        {
            UIBase ui = uiDic[uiName];
            ui.OnHide();
        }
     }
}
